<?php include 'inc/super_head.php';?>

    <div class="layui-container">

<fieldset class="layui-elem-field">
  <legend>已分配的班级（单击取消分配）</legend>
  <div class="layui-field-box">
        <form action="" class="layui-form layui-form-pane" id="form2">
           <?php if(!empty($this->assigned_classes)){?>
                <?php foreach ($this->assigned_classes as $key=>$v) { ?>
                    <span class="layui-btn" style="margin-left:10px;width:80px;">
                        <?=$key?>
                    </span>
                    <i class="glyphicon glyphicon-chevron-right"></i>
                    <?php foreach ($v as $key2=>$v2) { ?>
                    <input checked="" lay-filter="assigned" name="<?=$v2[0]?>" title="<?=$v2[1]?>" type="checkbox">
                    <?php }?>
                    <br><br>
                <?php }?>
            <?php }else{?>
                <span class="y-red">没有已分配的班级</span>
            <?php }?>
        </form>
  </div>
</fieldset>


<form action="" class="layui-form layui-form-pane" id="form3">
<fieldset class="layui-elem-field">
  <legend> 未分配的班级 </legend>
  <div class="layui-field-box">

        <?php if(!empty($this->free_classes)){?>
            <?php foreach ($this->free_classes as $v) { ?>
                <input lay-filter="noAssign" name="<?=$v['id']?>" title="<?=$v['name']?>" type="checkbox">
            <?php } ?>
        <?php }else{?>
            <span style="color:#999">没有未分配的班级</span>
        <?php }?>
        <br><br>

  <div class="layui-form-item">
    <label class="layui-form-label" style="width: 180px;">将选中的班级分配给</label>
    <div class="layui-input-inline">
      <select id="users" lay-filter="assignToTeacher" name="users">
                    <option value="">请选择老师</option>
                    <?php foreach ($this->users as $v){ ?>
                        <option value="<?=$v['id']?>"><?=$v['realname']?></option>
                    <?php }?>
      </select>
    </div>
    <label class="layui-form-label">其他操作</label>
    <div class="layui-input-inline" style="padding:3px;border:1px solid #eee;width:220px;text-align: center;">
        <button id='newClass' class="layui-btn layui-btn-sm layui-btn-normal">新增班级</button>
        <button lay-submit class="layui-btn layui-btn-sm layui-btn-danger" id="delBtn" lay-filter="delClass">删除选中班级</button>
    </div>
  </div>


  </div>
</fieldset>
</form>





    <script language="javascript">
        layui.use(['form', 'element'], function () {
            let form = layui.form
                , $ = layui.$;
            $("#itemClasses").addClass('layui-this'); //设置导航当前选中项的样式


            //begin_分配班级
            form.on('select(assignToTeacher)', function (data) {
                let idArray = [], teacher_id = data.value;
                $('#form3').find("input:checked").each(function () {
                    idArray.push($(this)[0]['name']);
                });
                if (idArray.length === 0) {
                    layer.msg('请选择未分配的班级');
                } else {
                    if (teacher_id === '') {
                        layer.msg('请选择教师');
                    } else {
                        $.post('/super/assignClass', {'id': idArray, 'teacher_id': teacher_id}, function (r) {
                            location.reload();
                        });
                    }
                }
            });
            //end_分配班级

            // begin 取消分配
            form.on('checkbox(assigned)', function (data) {
                $.post('/super/cancelAssignClass', {id: data.elem.name}, function (r) {
                    location.reload();
                });
            });
            // end 取消分配

            //begin_删除班级
            form.on('submit(delClass)', function () {
                let idArray = [];
                $('#form3').find("input:checked").each(function () {
                    idArray.push($(this)[0]['name']);
                });
                console.log(idArray);
                if (idArray.length === 0) {
                    layer.msg('请选择班级');
                } else {
                    layer.confirm('确定要删除这些班级吗?', {icon: 3}, function () {
                        $.post('/super/delClass', {'id': idArray}, function () {
                            location.reload();
                        });
                    });
                }
                return false;
            });
            //end_删除班级

            $('#newClass').click(function(){
                layer.open({
                        type: 1
                        , title: '<h3 style="color:#009688">新增班级</h3>'
                        , content: $('#divNewClass')
                        , area: ['850px', '400px']
                    });
                return false;
            });

            // 使用班级模板
            $('.mb').click(function(){
                $('#classes').val($(this).attr('value'));
            });

            //begin_创建班级
            form.on('submit(btn_addClass)', function (data) {
                let classes = data.field['classes'];
                console.log(classes);
                $.post('/super/addClass', {classes: classes}, function (r) {
                    layer.msg('创建成功！',function(){
                        location.reload();
                    })

                });
                return false;
            });
            //end_创建班级
        });
    </script>
    </div>

<?php include 'inc/super_foot.php';?>

<div id='divNewClass' style="display: none;width:850px;text-align: center">
    <div style="height: 50px;">&nbsp;</div>
    <form action="" class="layui-form layui-form-pane" id="frmAddClass">
        <div class="layui-form-item" style="text-align: center;display: inline-block;">
            <textarea class="layui-textarea" id="classes" lay-verify="required" name="classes" placeholder="
温馨提示：请在此输入新班级的名称
1、批量创建时，班级名称以英文点号分隔
2、班级名字后不需要加“班”字，以免重复" style="width: 700px;"></textarea>
        </div>
        <div class="layui-form-item" style="margin-top: 50px;">
            <button class="layui-btn" lay-filter="btn_addClass" lay-submit="">
                确 定 新 增
            </button>
            <span class="layui-btn layui-btn-primary mb" value='高一01.高一02.高一03.高一04.高一05.高一06.高一07.高一08.高一09.高一10.高一11'>

                使用"高一"模板
            </span>
            <span class="layui-btn layui-btn-primary mb" value='高二01.高二02.高二03.高二04.高二05.高二06.高二07.高二08.高二09.高二10.高二11'>
                使用"高二"模板
            </span>
            <span class="layui-btn layui-btn-primary mb" value='初一01.初一02.初一03.初一04.初一05.初一06.初一07.初一08.初一09.初一10.初一11'>
                使用"初一"模板
            </span>
            <span class="layui-btn layui-btn-primary mb" value='初二01.初二02.初二03.初二04.初二05.初二06.初二07.初二08.初二09.初二10.初二11'>
                使用"初二"模板
            </span>
        </div>


    </form>
</div>